#!/bin/bash
#####################################setup ros###################################
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt update
sudo apt install ros-melodic-desktop-full
sudo apt install rospack-tools
sudo apt-get install python-rosdep
sudo rosdep init
rosdep update
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
sudo apt install python-rosinstall python-rosinstall-generator python-wstool build-essential
######################################remove opencv##############################
pkg-config --modversion opencv
sudo apt-get purge libopencv*
sudo apt autoremove
sudo apt-get update
sudo rm -rf  /usr/local/share/opencv*
sudo rm -rf  /usr/local/include/opencv*
sudo rm -rf  /usr/local/lib/libopencv_*
sudo rm -rf  /usr/local/lib/cmake/opencv4/
sudo rm -rf /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
sudo apt remove opencv-data python-opencv
#######################################install base###################### 
sudo apt-get install build-essential -y
sudo apt-get install cmake git g++ -y
sudo apt-get install libboost-all-dev -y
sudo apt -y install libvorbis-dev libxvidcore-dev -y
sudo apt -y install libopencore-amrnb-dev libopencore-amrwb-dev -y
sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev -y
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev -y
sudo apt-get install checkinstall yasm libxine2-dev libfaac-dev libmp3lame-dev libtheora-dev -y
sudo apt-get install libavresample-dev x264 v4l-utils -y
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev -y
sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"
sudo apt update
sudo apt install libjasper1 libjasper-dev -y
sudo apt-get install libatlas-base-dev -y
sudo apt-get install libqglviewer-dev libsuitesparse-dev libgoogle-glog-dev libgflags-dev -y
sudo apt-get install python-dev python-numpy -y
sudo apt-get install git libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev -y
##################################################download######################################
#eigen
#git clone https://github.com/libeigen/eigen.git
wget https://gitlab.com/libeigen/eigen/-/archive/3.3.9/eigen-3.3.9.zip
unzip eigen-3.3.9.zip
cd ..
#opencv
mkdir opencv_cuda
cd opencv_cuda
wget https://github.com/opencv/opencv/archive/refs/tags/3.4.5.zip
unzip 3.4.5.zip
mv 3.4.5.zip opencv-3.4.5.zip
wget https://github.com/opencv/opencv_contrib/archive/refs/tags/3.4.5.zip
unzip 3.4.5.zip
mv 3.4.5.zip opencv_contrib-3.4.5.zip
cd ..
#fmt
wget https://github.com/fmtlib/fmt/releases/download/8.1.1/fmt-8.1.1.zip
unzip fmt-8.1.1.zip
#Sophus
git clone https://github.com/strasdat/Sophus.git
#ceres-solver 1.14.0
mkdir ceres-solver-1.14.0
wget https://ceres-solver.googlesource.com/ceres-solver/+archive/facb199f3eda902360f9e1d5271372b7e54febe1.tar.gz
tar -zxvf facb199f3eda902360f9e1d5271372b7e54febe1.tar.gz -C ./ceres-solver-1.14.0
mv facb199f3eda902360f9e1d5271372b7e54febe1.tar.gz ceres-solver-1.14.0.tar.gz
#gtsam
git clone https://bitbucket.org/gtborg/gtsam.git
#cv_bridge
wget https://github.com/ros-perception/vision_opencv/archive/refs/heads/melodic.zip
mv melodic.zip melodic_cv_bridge.zip
unzip melodic_cv_bridge.zip
#realsense
wget https://github.com/IntelRealSense/librealsense/archive/refs/tags/v2.48.0.zip
unzip v2.48.0.zip
########################build#######################
#eigen
cd eigen-3.3.9
mkdir build
cd build
cmake ..
sudo make install 
cd ..
cd ..
#opencv
#https://github.com/opencv/opencv/tree/3.4.5
#https://github.com/opencv/opencv_contrib/tree/3.4.5
cd opencv_cuda/opencv-3.4.5
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr -D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so -D CUDA_ARCH_BIN=6.2 -D CUDA_ARCH_PTX="" -D WITH_CUDA=ON     -D WITH_TBB=ON  -D BUILD_NEW_PYTHON_SUPPORT=ON  -D WITH_V4L=ON -D WITH_OPENGL=ON  -D WITH_GSTREAMER=ON -D WITH_GTK=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1  -D WITH_CUBLAS=1 -D WITH_NVCUVID=ON -D BUILD_opencv_cudacodec=OFF -D OPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_EXTRA_MODULES_PATH= ../../opencv_contrib-3.4.5/modules ../../opencv-3.4.5
sudo make -j4
sudo make install
cd ..
cd ..
cd ..
#fmt
#下载8.1.1版本
#https://fmt.dev/8.1.1/index.html
cd fmt-8.1.1
mkdir build
cd build
cmake ..
make 
sudo make install
cd ..
cd ..
#Sophus
#修改cmake文件，将fmt文件拷贝到Sophus目录下，添加set(fmt_DIR ./fmt_source/lib/cmake/fmt)
#if(NOT TARGET fmt::fmt)
#  set(fmt_DIR ./fmt_source/lib/cmake/fmt)
#  find_package(fmt)
#endif()
cd Sophus/
mkdir build
cd build
cmake .. 
make
sudo make install
cd ..
cd ..
#ceres
#ceres http://www.ceres-solver.org/installation.html#linux
cd ceres-solver-1.14.0
mkdir build
cmake ..
make
make install
#gtsam
cd gtsam
mkdir build
cd build
cmake ..
make 
make install

#cv_bridge
cd vision_opencv-melodic
cd  cv_bridge 
sed -i 's|OpenCV 3|OpenCV 3.4.5|' CMakeLists.txt
mkdir build 
cd build 
cmake ..
make
sudo cp -rpf devel/lib/libcv_bridge.so /opt/ros/melodic/lib/
sudo cp -rpf devel/lib/cv_bridge.pc  /opt/ros/melodic/lib/

#realsense 
cd librealsense-2.48.0
./scripts/setup_udev_rules.sh  
mkdir build && cd build  
cmake .. -DBUILD_EXAMPLES=true -DCMAKE_BUILD_TYPE=release -DFORCE_RSUSB_BACKEND=false -DBUILD_WITH_CUDA=true && make -j$(($(nproc)-1)) && sudo make install
